/*
 *  Copyright 2009 Matthieu Chase Heimer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.macro10.xroads.commands;

import com.macro10.switchboard.Request;
import com.macro10.switchboard.Session;
import com.macro10.switchboard.service.UserTransaction;
import com.macro10.xroads.entity.Area;
import com.macro10.xroads.entity.Avatar;
import com.macro10.xroads.service.AreaService;
import com.macro10.xroads.service.AvatarService;
import com.macro10.xroads.xml.ElementParser;

/**
 * Requires headers: avatar
 * Filters: require_login
 */
public class EnterWorld extends AbstractMoveCommand {

    @Override
    public ExecuteResult execute(Request request, Session session) {
        String username = (String) session.getAttribute("username");
        String avatarName = request.getHeader("avatar");

        UserTransaction ut = null;
        try {
            ut = new UserTransaction();
            AvatarService avatarService = new AvatarService(ut);
            Avatar avatar = avatarService.getAvatar(avatarName);
            if (avatar == null) {
                session.writeError(request.getURL() + ":avatar.not.found");
                ut.commit();
            } else if (avatar.getAccount() == null) {
                session.writeError(request.getURL() + ":avatar.missing.account");
                ut.commit();
            } else if (!avatar.getAccount().getClientId().equals(username)) {
                session.writeError(request.getURL() + ":avatar.not.yours");
                ut.commit();
            } else if (avatar.isOnline()) {
                session.writeError(request.getURL() + ":avatar.already.online");
                ut.commit();
            } else {
                avatar.setOnline(true);
                String areaName = null;
                if (avatar.getArea() == null) {
                    String elementName = (String) avatar.getObject("element");
                    areaName = ElementParser.getInstance().getElement(elementName).getDefaultArea();
                    AreaService areaService = new AreaService(ut);
                    Area targetArea = areaService.getArea(areaName);
                    avatar.setArea(targetArea);
                } else {
                    areaName = avatar.getArea().getAreaName();
                }

                session.writeText(request.getURL() + ":world.entered");
                showArea(session, avatar.getArea(), avatar);
                ut.commit();
                session.setAttribute("avatar", avatarName);
                session.setAttribute("area", areaName);

            }
        } catch (Exception e) {
            session.writeException(request.getURL() + ":world.enter.failed", e);
            if (ut != null) {
                ut.rollback();
            }
        }
        return ExecuteResult.START_POST;
    }
}
